
package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.ServiceSheet;
import com.sasis.dao.ServiceSheetDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class ServiceSheetDaoiBatis extends BaseDaoiBATIS implements ServiceSheetDao {

    /**
     * @see com.sasis.dao.ServiceSheetDao#getServiceSheets(com.sasis.model.ServiceSheet)
     */
    public List getServiceSheets(final ServiceSheet serviceSheet) {
          return getSqlMapClientTemplate().queryForList("getServiceSheets", serviceSheet);
    }

    /**
     * @see com.sasis.dao.ServiceSheetDao#getServiceSheet(Integer id)
     */
    public ServiceSheet getServiceSheet(Integer id) {
        ServiceSheet serviceSheet = (ServiceSheet) getSqlMapClientTemplate().queryForObject("getServiceSheet", id);

        if (serviceSheet == null) {
            throw new ObjectRetrievalFailureException(ServiceSheet.class, id);
        }

        return serviceSheet;
    }

    /**
     * @see com.sasis.dao.ServiceSheetDao#saveServiceSheet(ServiceSheet serviceSheet)
     */    
    public void saveServiceSheet(final ServiceSheet serviceSheet) {
        Integer id = serviceSheet.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addServiceSheet", serviceSheet);
        } else {
            getSqlMapClientTemplate().update("updateServiceSheet", serviceSheet);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(ServiceSheet.class, id);
        }
    }

    /**
     * @see com.sasis.dao.ServiceSheetDao#removeServiceSheet(Integer id)
     */
    public void removeServiceSheet(Integer id) {
        getSqlMapClientTemplate().update("deleteServiceSheet", id);
    }
}
